package org.darkforest.merchant.controller;

import org.darkforest.common.services.GoodsClassRedisService;
import org.darkforest.dao.mapper.GoodsClassMapper;
import org.darkforest.dao.redis.GoodsClassRedisDao;
import org.darkforest.model.GoodsClass;
import org.darkforest.ucenter.auth.AuthPassport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2017/4/1.
 */
@Controller
@RequestMapping(value = "gc")
public class GoodsClassController {

    @Autowired
    private GoodsClassRedisService goodsClassRedisService;
    @Autowired
    private GoodsClassMapper goodsClassMapper;
    @Autowired
    private GoodsClassRedisDao goodsClassRedisDao;
    Logger logger = LoggerFactory.getLogger(GoodsClassController.class);
    /**
     * 根据分类父ID获取子分类
     * @param pid
     * @return
     */
    @RequestMapping(value = "findChildrensByPid")
    public @ResponseBody
    List<GoodsClass> findChildrensByPid(Integer pid){
        List<GoodsClass> goodsClassList = goodsClassRedisService.getGoodsClassList(pid);
        return goodsClassList;
    }

    @AuthPassport
    @RequestMapping(value = "flushGoodsClassRedis")
    public @ResponseBody boolean flushGoodsClassRedis(){
        boolean res = true;
        try{
            GoodsClass goodsClassParam = new GoodsClass();
            goodsClassParam.setGcParentId(0);
            List<GoodsClass> parentClass = goodsClassMapper.findGoodsClass(goodsClassParam);
            if (parentClass != null) {
                goodsClassRedisDao.deleteList("gc_list");
                goodsClassRedisDao.addClassList("gc_list", parentClass);
                logger.debug("----add gc_list----");
                for (GoodsClass cls : parentClass) {
                    logger.debug("----gc_list:" + cls.getGcId() + ",name:" + cls.getGcName() + "-----");
                    goodsClassParam.setGcParentId(cls.getGcId());
                    List<GoodsClass> level2Class = goodsClassMapper.findGoodsClass(goodsClassParam);
                    if (level2Class != null) {
                        goodsClassRedisDao.deleteList("gc_list_" + cls.getGcId());
                        goodsClassRedisDao.addClassList("gc_list_" + cls.getGcId(), level2Class);
                        for (GoodsClass subCls : level2Class) {
                            logger.debug("----sub gc_list:" + subCls.getGcId() + ",sub cls name:" + subCls.getGcName() + "-----");
                            goodsClassParam.setGcParentId(subCls.getGcId());
                            List<GoodsClass> level3Class = goodsClassMapper.findGoodsClass(goodsClassParam);
                            if (level3Class != null) {
                                goodsClassRedisDao.deleteList("gc_list_" + subCls.getGcId());
                                goodsClassRedisDao.addClassList("gc_list_" + subCls.getGcId(), level3Class);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            res =false;
        }finally {
            return res;
        }

    }

}
